﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FraudMitigator
{
    public partial class EditUsersForm : Form
    {
        UserData data;

        public EditUsersForm()
        {
            InitializeComponent();
            dataGridView1.DataSource = Queries.editUsersFormQuery();

            // change the column headers to properly capitalized text
            foreach (int pos in Strings.editUsersColumnNames.Keys)
            {
                dataGridView1.Columns[pos].HeaderText = (string)Strings.editUsersColumnNames[pos];
            }
        }

        private void SaveData(UserData d)
        {
            data = d;
        }

        private void changeBtn_Click(object sender, EventArgs e)
        {
            data.newFirstName = firstNameTxtBox.Text;
            data.newLastName = lastNameTxtBox.Text;
            data.newUsername = usernameTxtBox.Text;
            data.newPassword = passwordTxtBox.Text;
            data.newAccessLevel = accessLvlTxtBox.Text;
            Queries.updateUserQuery(data);

            // refresh the view
            dataGridView1.DataSource = Queries.editUsersFormQuery();
            Strings.Log(MainScreen.user.UserUsername + " modified a user record.");
        }

        private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            if (e.StateChanged == DataGridViewElementStates.Selected)
            {
                UserData data = new UserData();
                firstNameTxtBox.Text = data.firstName = e.Row.Cells[0].Value.ToString();
                lastNameTxtBox.Text = data.lastName = e.Row.Cells[1].Value.ToString();
                usernameTxtBox.Text = data.username = e.Row.Cells[2].Value.ToString();
                passwordTxtBox.Text = data.password = e.Row.Cells[3].Value.ToString();
                accessLvlTxtBox.Text = data.accessLevel = e.Row.Cells[4].Value.ToString();

                SaveData(data);
            }
        }
    }
}
